package logic

import (
	"context"
	"user/appconfig"
	"user/userclient"

	"api/internal/svc"
	"api/internal/types"
	"github.com/golangblogs/gojwt"
	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LonginLogic) Longin(req *types.Longinrequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	longin, err := l.svcCtx.UserRpc.Longin(l.ctx, &userclient.LonginRequest{
		Account:  req.Account,
		Password: req.Password,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	newjwt := jwt.NewJWT(appconfig.AppConf.Jwt.APP_KEY)
	claims := jwt.CustomClaims{ID: uint(longin.Id)}
	token, _ := newjwt.CreateToken(claims)

	return &types.Response{
		Code: 200,
		Msg:  "登陆成功",
		Data: map[string]interface{}{
			"token":   token,
			"user_id": longin.Id,
		},
	}, nil
}
